/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.leeloo.databinding.internal.ui;

import junit.framework.TestCase;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.core.Tags;

/**
 * @author Brad Reynolds
 */
public class LabeledTagTreeNodeCellModifierTest extends TestCase {
    private TreeViewer treeViewer;
    private Shell shell;
    
    protected void setUp() throws Exception {
        shell = new Shell();
        treeViewer = new TreeViewer(shell, SWT.NONE);
    }
    
    protected void tearDown() throws Exception {
        if (shell != null && !shell.isDisposed()) {
            shell.dispose();
        }
    }
    
    /**
     * Test method for {@link org.leeloo.databinding.internal.ui.LabeledTagTreeNodeCellModifier#canModify(java.lang.Object, java.lang.String)}.
     */
    public void testCanModify() {
        LabeledTreeNode node = new LabeledTreeNode(null);
        LabeledTagTreeNodeCellModifier modifier = new LabeledTagTreeNodeCellModifier(treeViewer, new TagService());
        assertFalse(modifier.canModify(node, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY));
        
        node = new LabeledTagTreeNode("", new Tag(Tags.NAME, ""), true);
        assertTrue(modifier.canModify(node, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY));
        
        node = new LabeledTagTreeNode("", new Tag(Tags.NAME, ""), false);
        assertFalse(modifier.canModify(node, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY));
    }

    /**
     * Test method for {@link org.leeloo.databinding.internal.ui.LabeledTagTreeNodeCellModifier#getValue(java.lang.Object, java.lang.String)}.
     */
    public void testGetValue() {
        TagService tagService = new TagService();
        LabeledTagTreeNodeCellModifier modifier = new LabeledTagTreeNodeCellModifier(treeViewer, tagService);
        Object object = new Object();
        
        LabeledTagTreeNode node = new LabeledTagTreeNode(object, new Tag(Tags.NAME, ""), true);
        assertEquals("", modifier.getValue(node, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY));
        
        String name = "a name";
        tagService.put(object, new Tag(Tags.NAME, name));
        assertEquals(name, modifier.getValue(node, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY));
    }

    /**
     * Test method for {@link org.leeloo.databinding.internal.ui.LabeledTagTreeNodeCellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)}.
     */
    public void testModify() {
        TagService tagService = new TagService();
        LabeledTagTreeNodeCellModifier modifier = new LabeledTagTreeNodeCellModifier(treeViewer, tagService);
        Object object = new Object();
        LabeledTagTreeNode node = new LabeledTagTreeNode(object, new Tag(Tags.NAME, ""), true);
        
        assertNull(tagService.get(object, Tags.NAME));
        String value = "value";
        
        TreeItem item = new TreeItem(treeViewer.getTree(), SWT.NONE);
        item.setData(node);
        modifier.modify(item, LabeledTagTreeNodeCellModifier.VALUE_PROPERTY, value);
        
        Tag tag = tagService.get(object, Tags.NAME);
        assertNotNull(tag);
        assertEquals(value, tag.getValue());
    }
}
